package someTestExcemple.huaweiOd.container.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class OdTest01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String input = sc.nextLine();
        input = input.substring(1,input.length()-1);
        String[] arr = input.split("],");

        int n = arr.length;

        int[][] meetings = new int[n][2];
        for (int i = 0; i < n; i++) {
            String[] temp = arr[i].replace("[","").replace("]","").split(",");
            meetings[i][0] = Integer.valueOf(temp[0].trim());
            meetings[i][1] = Integer.valueOf(temp[1].trim());
        }

        // 进行会议时间的合并
        List<int[]> mergedMeetings = mergeIntervals(meetings);

        // 构建输出格式
        StringBuilder result = new StringBuilder("[");
        for (int[] meeting : mergedMeetings) {
            if (result.length() > 1) result.append(", ");
            result.append("[").append(meeting[0]).append(", ").append(meeting[1]).append("]");
        }
        result.append("]");

        System.out.println(result);
    }

    private static List<int[]> mergeIntervals(int[][] intervals) {
        // 对区间按照开始时间进行排序
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

        List<int[]> merged = new ArrayList<>();
        for (int[] interval : intervals) {
            // 如果列表为空或者当前区间与列表中最后区间不重叠，则直接添加
            if (merged.isEmpty() || merged.get(merged.size() - 1)[1] < interval[0]) {
                merged.add(interval);
            } else {
                // 否则，合并当前区间与最后一个区间
                merged.get(merged.size() - 1)[1] = Math.max(merged.get(merged.size() - 1)[1], interval[1]);
            }
        }
        return merged;
    }
}

